import './App.css'
import { getAllData, getPie } from './dataapi'

function App() {
  const handleLotteryDataDownload = async () => {
    const data = await getAllData()
    const blob = new Blob([JSON.stringify(data)], { type: 'application/json' })
    const url = URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.download = 'lottery-data.json'
    link.href = url
    link.click()
    URL.revokeObjectURL(url)

    const simpleData = data.map((item) => ({
      lotterDrawNum: item.lotteryDrawNum,
      lotteryDrawResult: item.lotteryDrawResult,
      lotteryDrawTime: item.lotteryDrawTime,
    }))

    const blob2 = new Blob([JSON.stringify(simpleData)], { type: 'application/json' })
    const url2 = URL.createObjectURL(blob2)
    const link2 = document.createElement('a')
    link2.download = 'lottery-data-simple.json'
    link2.href = url2
    link2.click()
    URL.revokeObjectURL(url2)
      
  }
  return (
    <>
      <div>
      </div>
      <h1>Vite + React</h1>
      <div className="card">
        <button onClick={handleLotteryDataDownload}>
          get lottery data
        </button>
        <button onClick={
          async () => {
            const data = await getPie()
            console.log(data);
          }
        }>covernt</button>
      </div>
    </>
  )
}

export default App
